﻿using Newtonsoft.Json;
using PlanckTime.Entity;
using System.Web.Script.Serialization;

namespace Microsoft.ManagementConsole.SnapIns.Serialization
{
    public class JsonSerialization : ISerialization
    {
        #region Fields&Properties

        #endregion

        #region Methods

        public string Serialize<T>(T data)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer(new CustomResolver());

            return serializer.Serialize(data);
        }

        public T Deserialize<T>(string data)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer(new SimpleTypeResolver());

            return serializer.Deserialize<T>(data);
        }

        #endregion
    }

    public class CustomResolver : JavaScriptTypeResolver
    {

        public override System.Type ResolveType(string id)
        {
            return null;
        }

        public override string ResolveTypeId(System.Type type)
        {

            if (type.Namespace == typeof(User).Namespace)
            {
                return type.AssemblyQualifiedName;
            }

            return null;
        }
    }
}
